interface Player {
  bankerCount: number,
  lftId: number,
  nickname: string,
  owner: boolean,
  point: number,
  seatIndex: number,
  suitDescribe: Array<{name: string, count: number}>,
  suitMap: any,
  suitPatterns: any,
  uid: string,
  winner: boolean,
  headUrl: string
}

declare var window: any                                                                                                            

class UserLabel extends eui.Component {
  private userGroup: eui.Group
  private labelBg: eui.Image
  private userid: eui.Label
  private typeDesc: eui.Label
  private userContent: eui.Group
  private userScore: eui.Label
  private username: eui.Label
  public player: Player
  public idFrame: eui.Image
  public headUrl: eui.Image
  public mask: eui.Image
  public headUrlFrame: eui.Image
  constructor(player) {
    super()
    this.once(eui.UIEvent.COMPLETE, this.onComplete, this)
    this.player = player
    this.skinName = `<e:Skin class="MajiangSkin" width="667" height="80" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
      <e:Image id="labelBg" x="24" y="0" width="620" height="80" ></e:Image>
      <e:Group id="userGroup" width="620" height="80">
        <e:Image id="headUrlFrame" x="47" y="7" width="63" height="56"></e:Image>
        <e:Image id="headUrl" x="50" y="10" width="56" height="50"></e:Image>
        <e:Image id="mask" x="50" y="10" width="56" height="50"></e:Image>

        <e:Label id="username" width="150" text="" textColor="0x82511c" size="23" x="122" y="16" textAlign="left" height="30"></e:Label>
        <e:Image id="idFrame" x="122" y="38" width="22" height="22"></e:Image>
        <e:Label id="userid" text="31231" size="23" height="53" x="150" y="38" textColor="0x82511c"></e:Label>
        <e:Group id="userContent" x="0" y="87" width="180" height="168">
        </e:Group>
        <e:Label id="userScore" text="" size="44" height="53" left="498" y="20" textColor="0x82511c"></e:Label>
      </e:Group>
    </e:Skin>`
  }

  private onComplete(e: eui.UIEvent) {
    const player = this.player
    const bgSrc = player.point >= 0 ? 'ten-light_png' : 'ten-default_png'
    this.labelBg.source = bgSrc

    this.username.text = this.player.nickname
    this.userid.text = this.player.lftId + ''
    this.idFrame.source = 'id-frame_png'
    this.mask.source = 'mask_png'
    this.headUrlFrame.source = 'headurl-frame_png'
    if (window.location.href.match('10.10.10')) {
      this.headUrl.source = this.player.headUrl.replace('./', 'https://c-c.jiahuagame.com/console/')
    } else {
      this.headUrl.source = this.player.headUrl
    }
    this.headUrl.mask = this.mask

    if (this.player.winner) {
      let winner = new eui.Image('big-winner-ten_png')
      winner.right = 128
      winner.bottom = 26
      winner.height = 46
      winner.width = 76
      this.userGroup.addChild(winner)
    }
    if (this.player.owner) {
      let owner = new eui.Image('owner_niuniu_png')
      owner.left = 18
      owner.top = 6
      owner.height = 64
      owner.width = 42
      this.userGroup.addChild(owner)
    }

    if (this.player.point >= 0) {
      this.userScore.textColor = 0xdf3b0f
      this.userScore.text = '+' + this.player.point
    } else {
      this.userScore.textColor = 0x12c52f
      this.userScore.text = '' + this.player.point
    }


    if (this.$hasAddToStage) {
      window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
    } else {
      this.once(eui.UIEvent.ADDED_TO_STAGE, function () {
        setTimeout(() => {
          window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
        }, window.labelRenderTime)
      }, this)
    }
  }
}

export default UserLabel